package com.fighthouse.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;

import com.fighthouse.entity.Banner;


public class BannerService {
	private static final String PERSISTENCE_UNIT = "fight_site";
	private static Logger logger = Logger.getLogger(Banner.class);
	@SuppressWarnings("unchecked")
	public List<Banner> getList(){
		logger.debug("** Chamada aos banners **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();
		
		
		//Busca por named query e hql
        List<Banner> itens = (List<Banner>) em.createNamedQuery("banners.getList").getResultList();
		if(itens != null)logger.debug("** Encontrados " + itens.size() + " itens");
		
		
		List<Banner> itensRef = new ArrayList<Banner>(); 
		
		for(int i = 0;i < itens.size(); i++){
			if(itens.get(i).getInicio() != null && itens.get(i).getFim() != null){
				if(itens.get(i).getInicio().getTimeInMillis() <= System.currentTimeMillis()&& itens.get(i).getFim().getTimeInMillis() > System.currentTimeMillis()){
					itensRef.add(itens.get(i));
				}
			}
		}
		
		if(itensRef != null)logger.debug("** Quantidade de itens na data " + itensRef.size() + " itens");
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return itensRef;
	}
	@SuppressWarnings("unchecked")
	public List<Banner> getFullList(){
		logger.debug("** Chamada aos banners **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();
		
		
		//Busca por named query e hql
        List<Banner> itens = (List<Banner>) em.createNamedQuery("banners.getList").getResultList();
		if(itens != null)logger.debug("** Encontrados " + itens.size() + " itens");
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return itens;
	}
	public Banner update(Banner ban){
		logger.debug("** update banner **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();

		EntityTransaction tx = em.getTransaction();
		
		tx.begin();
		
		try{
			ban = em.merge(ban);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			System.out.println(e.getMessage());
		}
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return ban;
	}
	public int delet(List<Banner> bans){
		logger.debug("** delete banners **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();

		EntityTransaction tx = em.getTransaction();
		
		tx.begin();
		int i = 0;
		try{
			for(Banner ban:bans){
				em.remove(em.merge(ban));
				i++;
			}
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			System.out.println(e.getMessage());
		}
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return i;
	}
}
